package com.avast.personalbudgetapp.presenter.activity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;

import com.avast.personalbudgetapp.R;
import com.avast.personalbudgetapp.global.constant.Constants;
import com.avast.personalbudgetapp.global.util.AppUtil;
import com.avast.personalbudgetapp.model.bean.Proto.Transaction;
import com.avast.personalbudgetapp.model.bean.TransactionDate;

public class TransactionAddOrEditActivity extends AbstractActivity {
	
	private String transactionGuid;
	
	public TransactionAddOrEditActivity(){
		super(R.layout.fragment_transaction_edit, R.menu.transaction_edit);
	}
	
	/**
	 * Initializes the activity with view objects
	 */	
	protected void init(){
		
	    Intent intent = getIntent();
	    String editMode = intent.getStringExtra(Constants.INTENT_TRANSACTION_EDIT_MODE);
	    
	    if(editMode != null && editMode.equals(Constants.TRANSACTION_UPDATE)){
	    	
		    transactionGuid = intent.getStringExtra(Constants.INTENT_TRANSACTION_GUID);
		    String kind = intent.getStringExtra(Constants.INTENT_TRANSACTION_KIND);
		    String value = intent.getStringExtra(Constants.INTENT_TRANSACTION_VALUE);
		    String date = intent.getStringExtra(Constants.INTENT_TRANSACTION_DATE);
		    
		    setContentView(R.layout.fragment_transaction_edit);
		    
			EditText editTextKind = (EditText) findViewById(R.id.editTextKind);
			EditText editTextValue = (EditText) findViewById(R.id.editTextValue);
			DatePicker datePicker = (DatePicker) findViewById(R.id.datePicker1);
			Button btnRemove = (Button) findViewById(R.id.btnRemove);
			
			editTextKind.setText(kind);
			editTextValue.setText(value);
			
			TransactionDate transDate = AppUtil.getTransactionDate(date);
			datePicker.init(transDate.getYear(), transDate.getMonth(), transDate.getDay(), null);
			
			btnRemove.setVisibility(View.VISIBLE);
			
	    } else {
	    	setContentView(R.layout.fragment_transaction_edit);
	    }
		
	}
	
	/**
	 * Update selected transaction
	 * 
	 * @param view
	 */
	@SuppressLint("NewApi")
	public void updateTransaction(View view){
		
		EditText editTextKind = (EditText) findViewById(R.id.editTextKind);
		EditText editTextValue = (EditText) findViewById(R.id.editTextValue);
		DatePicker datePicker = (DatePicker) findViewById(R.id.datePicker1);
		
		Transaction.Builder transaction = Transaction.newBuilder();
		
		if(transactionGuid == null){
			transactionGuid = AppUtil.generateGuid();
		}
		
		transaction.setGuid(transactionGuid);
		transaction.setDate(datePicker.getCalendarView().getDate());
		transaction.setKind(editTextKind.getText().toString());
		transaction.setValue(Double.parseDouble(editTextValue.getText().toString()));
		
		appService.insertOrUpdate(transaction.build());
		startTransactionListActivity();
		
	}
	
	/**
	 * Remove selected transaction
	 * 
	 * @param view
	 */
	public void removeTransaction(View view){
		appService.removeTransaction(transactionGuid);
		startTransactionListActivity();
	}

}
